<?php

/**
 * Albireo Kernel
 *
 * @copyright  Copyright (c) 2010 Albireo Solutions
 * @package    Kernel
 */

namespace ApiModule;

use ApiModule\BasePresenter;
use Albireo\FileSystem\DataFile;

//use OAuthException;

use Nette\Application\Presenter;
use Nette\Security\AuthenticationException;

use Celebrio\FileSystem\InvalidVfsApiOperationException;
use Celebrio\FileSystem\FileSystemApi;

/**
 * Rest presenter.
 * Does not have to check parameters
 * rpcManagement have to take care of it
 * take care of authException????
 *
 * This API supports DELETE, PUT also but you can work only with GET or POST if you need by specifying actions
 * @author     petr
 * @package    Api
 */
//TODO: permissions to access API based on applications table
//TODO: react to PUT, DELETE headers, data type could be set by suffix (for example copy.json or copy.xml)
class RestPresenter extends BasePresenter {

    /**
     * @var Celebrio\FileSystem\FileSystemApi
     */
    private $fsApi;
//    private $oauth;

    /**
     * startup is always called
     */
    public function startup() {
        parent::startup();
        $this->fsApi = new FileSystemApi($this->getUser());
        //$this->oauth = new OAuthAccess();
    }

    private function setViewByType($view) {
        if (empty($view)) {
            $view = 'json';
        }
        $this->view = $view;
    }

    private function checkAccess() {
//        try {
//            if (!$this->oauth->checkAccess($this->getUser())) throw new AuthenticationException ("Authentication failed");
//        } catch (OAuthException $e) {
//            throw new AuthenticationException ("No access tokens sent", $e->getCode(),$e);
//        }
    }

    public function actionLoad($what, $out) {
        $this->setViewByType($out);
        //todo check it, please
        //$this->checkAccess();
        $dataFile = $this->fsApi->load($what);
        if (is_string($dataFile->getData())) {
            $this->template->data = $dataFile->getData();
        }
        if (is_array($dataFile->getData())) {
            $this->template->directory = $dataFile->getData();
        }
    }

    public function actionSave($what, $out, $data, $mimeType = null) {
//        $this->checkAccess();
        $this->setViewByType($out);
        $this->fsApi->save($what, $data, $mimeType);
    }

    public function actionGetFileInfo($what, $out) {
//        $this->checkAccess();
        $this->setViewByType($out);
        $this->template->file = $this->fsApi->getFileInfo($what);
    }

    public function actionCopy($what, $where, $out, $rewrite) {
//        $this->checkAccess();
        $this->setViewByType($out);
        $this->fsApi->copy($what, $where, $rewrite);
    }

    public function actionMove($what, $where, $out, $rewrite) {
//        $this->checkAccess();
        $this->setViewByType($out);
        $this->fsApi->move($what, $where, $rewrite);
    }

    public function actionDelete($what, $out) {
//        $this->checkAccess();
        $this->setViewByType($out);
        $this->fsApi->delete($what);
    }
}